<template>
  <div class="app-container">
    <div class="module-title">
      <svg-icon icon-class="chart" style="font-size:28px;margin-right:8px;"/>
      <span>报表管理</span>
      <span class="module-desc">管理供应商应付款月度报表</span>
    </div>
    <div class="module-card">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="供应商" prop="supplierName">
          <el-input
            v-model="queryParams.supplierName"
            placeholder="请输入供应商名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['report:report:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['report:report:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['report:report:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['report:report:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="reportList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="id" align="center" prop="id" />
        <el-table-column label="月份" align="center" prop="month" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.month, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="供应商名称" align="center" prop="supplierName" />
        <el-table-column label="付款方" align="center" prop="payer" />
        <el-table-column label="月结天数" align="center" prop="monthlySettlementDays">
          <template slot-scope="scope">
            <span class="quantity-amount">{{ scope.row.monthlySettlementDays || '0' }}天</span>
          </template>
        </el-table-column>
        <el-table-column label="对账截止日" align="center" prop="reconciliationDeadline" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.reconciliationDeadline, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="交易币种" align="center" prop="transactionCurrency" />
        <el-table-column label="应付金额" align="center" prop="payableAmount">
          <template slot-scope="scope">
            <span class="payable-amount">¥{{ scope.row.payableAmount || '0.00' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="已付金额" align="center" prop="paidAmount">
          <template slot-scope="scope">
            <span class="paid-amount">¥{{ scope.row.paidAmount || '0.00' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="尚欠款金额" align="center" prop="outstandingAmount">
          <template slot-scope="scope">
            <span class="outstanding-amount">¥{{ scope.row.outstandingAmount || '0.00' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['report:report:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['report:report:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <!-- 添加或修改应付款月份报对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="月份" prop="month">
              <el-date-picker clearable
                v-model="form.month"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择月份">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商名称" prop="supplierName">
              <el-input v-model="form.supplierName" placeholder="请输入供应商名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="付款方" prop="payer">
              <el-input v-model="form.payer" placeholder="请输入付款方" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="月结天数" prop="monthlySettlementDays">
              <el-input v-model="form.monthlySettlementDays" placeholder="请输入月结天数">
                <template slot="append">天</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="对账截止日" prop="reconciliationDeadline">
              <el-date-picker clearable
                v-model="form.reconciliationDeadline"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择对账截止日">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="交易币种" prop="transactionCurrency">
              <el-input v-model="form.transactionCurrency" placeholder="请输入交易币种" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="应付金额" prop="payableAmount">
              <el-input v-model="form.payableAmount" placeholder="请输入应付金额">
                <template slot="prepend">¥</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="已付金额" prop="paidAmount">
              <el-input v-model="form.paidAmount" placeholder="请输入已付金额">
                <template slot="prepend">¥</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="尚欠款金额" prop="outstandingAmount">
              <el-input v-model="form.outstandingAmount" placeholder="自动计算" disabled>
                <template slot="prepend">¥</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReport, getReport, delReport, addReport, updateReport } from "@/api/report/report"

export default {
  name: "Report",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 应付款月份报表格数据
      reportList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        supplierName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询应付款月份报列表 */
    getList() {
      this.loading = true
      listReport(this.queryParams).then(response => {
        this.reportList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        month: null,
        supplierName: null,
        payer: null,
        monthlySettlementDays: null,
        reconciliationDeadline: null,
        transactionCurrency: null,
        payableAmount: null,
        paidAmount: null,
        outstandingAmount: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push('/report/report/add')
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getReport(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改应付款月份报"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // 创建提交数据，排除自动计算的字段
          const submitData = {
            id: this.form.id,
            month: this.form.month,
            supplierName: this.form.supplierName,
            payer: this.form.payer,
            monthlySettlementDays: this.form.monthlySettlementDays,
            reconciliationDeadline: this.form.reconciliationDeadline,
            transactionCurrency: this.form.transactionCurrency,
            payableAmount: this.form.payableAmount,
            paidAmount: this.form.paidAmount,
            remark: this.form.remark
          }
          
          if (this.form.id != null) {
            updateReport(submitData).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addReport(submitData).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除应付款月份报编号为"' + ids + '"的数据项？').then(function() {
        return delReport(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('report/report/export', {
        ...this.queryParams
      }, `report_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>

<style scoped lang="scss">
@import "~@/assets/styles/module-card.scss";

.module-title {
  display: flex;
  align-items: center;
  font-size: 22px;
  font-weight: 700;
  color: #2980b9;
  margin-bottom: 18px;
  .module-desc {
    font-size: 14px;
    color: #888;
    font-weight: 400;
    margin-left: 18px;
  }
}

.payable-amount {
  font-weight: 600;
  color: #e74c3c;
  font-size: 14px;
}

.paid-amount {
  font-weight: 600;
  color: #27ae60;
  font-size: 14px;
}

.outstanding-amount {
  font-weight: 600;
  color: #f39c12;
  font-size: 14px;
}

.quantity-amount {
  color: #3498db;
  font-weight: 600;
  font-size: 14px;
}

.el-table {
  .el-table__body tr:hover > td {
    background: #eaf6ff !important;
  }
}

.el-dialog {
  .el-form-item {
    margin-bottom: 20px;
  }
  
  .el-input-group__prepend {
    background: #f0f4fa;
    color: #2980b9;
    font-weight: 600;
    border-color: #dbeafe;
  }
}
</style>
